package com.mine.distinctionteam.ui.home.adapter;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mine.distinctionteam.R;
import com.mine.distinctionteam.bean.home.ContentsBean;
import java.util.List;
/**
 * Created by loverforever on 2016/9/8.
 */
public class AdvertisementViewPagerAdapter extends PagerAdapter {
    private Context mContext;
    private List<ContentsBean> list=null;
    private LayoutInflater inflater;

    public AdvertisementViewPagerAdapter(Context mContext , List<ContentsBean> list){
        this.mContext=mContext;
        this.list=list;
        inflater= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view=inflater.inflate(R.layout.item_fragment_home_advertisement_viewpager,container,false);
        ImageView imageView_item_fragment_home_advertisement_image= (ImageView) view.findViewById(R.id.imageView_item_fragment_home_advertisement_image);
        Glide.with(mContext).load(list.get(position).getImage()).into(imageView_item_fragment_home_advertisement_image);
        container.addView(view);
        return view;
    }
    @Override
    //销毁item
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    //刷新GrilView数据
    public void reloadGridView(List<ContentsBean> _list, boolean isClear) {
        if (isClear) {
            list.clear();
        }
        list.addAll(_list);
        //刷新
        notifyDataSetChanged();
    }
    //Gson解析
    public ContentsBean parseJsonToList(String json) {
        //创建gson对象
        Gson gson = new Gson();
        //第一个参数 json字符串
        //第二个参数 HotBean对象
        //格式就是这么写的 有问题问gson工程师去
        return gson.fromJson(json, new TypeToken<ContentsBean>() {
        }.getType());
    }

}
